// pages/mine/coupon/coupon.js
const app = getApp();
const util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    useableCoupon: [],
    unUseableCoupon: []
  },

  // 选择优惠券
  choiceCoupon: function(e) {
    let that = this
    // app.globalData.couponid = 0
    // app.globalData.cost = 0
    let id = e.currentTarget.dataset.index
    // console.log(id)
    let useableCoupon = that.data.useableCoupon
    let cost = 0
    // for (let i = 0; i < useableCoupon.length; i++) {
      // if (useableCoupon[i].id = id) {
        // console.log(useableCoupon[id].id)
        // console.log(useableCoupon[id].cost)
        cost = useableCoupon[id].cost
      // }
    // }
    app.globalData.couponid = useableCoupon[id].id
    app.globalData.cost = cost
    wx.navigateBack({
      delta: 1
    })
    // console.log(app.globalData.couponid)
    // console.log(app.globalData.cost)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    app.globalData.cost = 0
    // 请求优惠券列表
    let wxCouponListUrl = app.globalData.baseUrl + "weixin/wxCouponList"
    let cid = wx.getStorageSync('cid')
    let data = {
      cid: cid,
      price: options.price,
      couponId: options.couponId
    }
    util.requestLoading(wxCouponListUrl, data, "GET").then(res => {
      console.log(res)
      that.setData({
        useableCoupon: res.data.useableCoupon,
        unUseableCoupon: res.data.unUseableCoupon
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})